package com.mall.canal.listener;

import com.mall.canal.job.JobCreate;
import com.mall.canal.job.SeckillActivityJob;
import com.mall.seckill.model.SeckillActivity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import java.text.SimpleDateFormat;

/**
 * @author Darren
 * @date 2022-10-08 17:46
 * Description：
 */
@Component
@CanalTable(value = "seckill_activity")
public class SeckillActivityHandler implements EntryHandler<SeckillActivity> {
    @Autowired
    private JobCreate jobCreate;

    @Override
    public void insert(SeckillActivity seckillActivity) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("ss mm HH dd MM ? yyyy");
        String cron = dateFormat.format(seckillActivity.getEndTime());
        System.out.println("cron ===> " + cron);
        jobCreate.create(seckillActivity.getId(), cron, 1, new SeckillActivityJob(), seckillActivity.getId());
    }

    @Override
    public void update(SeckillActivity before, SeckillActivity after) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("ss mm HH dd MM ? yyyy");
        String cron = dateFormat.format(after.getEndTime());
        System.out.println("cron ===> " + cron);
        jobCreate.create(after.getId(), cron, 1, new SeckillActivityJob(), after.getId());
    }

    @Override
    public void delete(SeckillActivity seckillActivity) {

    }
}
